<?php

namespace App\Http\Controllers\Admins;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admins\UserRequest;
use App\Repositories\BLL\UserBLL;
use App\Repositories\BLL\RoleBLL;
use App\Repositories\BLL\RoleUserBLL;
use Illuminate\Support\Facades\Route;

class UserController extends Controller
{
	private $userBLL,$roleBLL,$roleUserBLL;
	private $currentRouteName;
	  	

    function __construct(UserBLL $user,RoleBLL $role,RoleUserBLL $roleUser)
    {
    		$this->currentRouteName = Route::currentRouteName();
        $this->userBLL = $user;
        $this->roleBLL = $role;
        $this->roleUserBLL = $roleUser;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $addButton = sp_get_AddActionButton($this->currentRouteName);
        return view('admin.user.index',
        					["addButton"=>$addButton]);
    }
	
	public function ajaxIndex()
    {
        $result = $this->userBLL->ajaxIndex($this->currentRouteName);
        return response()->json($result);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
    		$roles = $this->roleBLL->getAllEntity();
        return view('admin.user.create',['roles'=>$roles]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(UserRequest $request)
    {
        $this->userBLL->createEntity($request->all());
        return redirect(route('user.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $user = $this->userBLL->editView($id);
        return view('admin.user.show',
						["user"=>$user]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
    		$roles = $this->roleBLL->getAllEntity();
    		$roleUsers = $this->roleUserBLL->getAllEntityByUser($id);
        $user = $this->userBLL->editView($id);
        return view('admin.user.edit',
						["user"=>$user,'roles'=>$roles,'roleusers'=>$roleUsers]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UserRequest $request, $id)
    {
        $this->userBLL->updateEntity($request->all(),$id);
        return redirect(route('user.index'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->userBLL->destroyEntity($id);
        return redirect(route('user.index'));
    }
	
	/**
	 * 重置密码
	 */
	public function resetPsd($id)
    {
        $this->userBLL->resetPsd($id);
        return redirect(route('user.index'));
    }
}
